<?php
namespace app\app\controller;
use think\Controller;
class Index extends Controller{	//留空
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    //申请应用，生成一对秘钥
    //https://open.omechain.co/app/index/create/
    //{name:XX商城,u_safe:baidu.com}
    public function create(){
    	$appdata=apijieshou();
        //合法性
        if(!isset($appdata['name'])){
            apifasong(['zt'=>'0','msg'=>'应用名称不能为空']);
        }elseif(!isset($appdata['u_safe'])){
            apifasong(['zt'=>'0','msg'=>'应用域名不能为空']);
        }
        $hasapp = db('app')->where('u_safe',$appdata['u_safe'])->find();
        if($hasapp){
            if($hasapp['zt']==0){
                apifasong(['zt'=>'0','msg'=>'该应用正在审核中']);
            }elseif($hasapp['zt']==1){
                apifasong(['zt'=>'0','msg'=>'该域名已被注册']);
            }elseif($hasapp['zt']==2){
                apifasong(['zt'=>'0','msg'=>'该应用已过期，请勿重复注册']);
            }
        }
        //业务
    	// $appid = db()->query('select uuid_short()');
    	// $appid = $appid[0]['uuid_short()'];
		$key = db()->query('select uuid()');
		$key = md5($key[0]['uuid()']);    //MD5 32位
		$secret_rand = apirand(50);     //50位随机数
		$secret_base = time().$secret_rand;   //原始值
		$secret = sha1(substr($secret_base,3,50));    //生成的secret sha1 40位

        $now = time();
        $data['key'] = $key;
        $data['secret'] = $secret;
        $data['name'] = $appdata['name'];
        $data['u_safe'] = $appdata['u_safe'];
        $data['ctime'] = $now;
        $data['utime'] = $now;
        $data['dtime'] = 0;
        $data['zt'] = 0;
        // dump($data);
        $ret = db('app')->insertGetId($data);
        if($ret){
            apifasong(['zt'=>'1','msg'=>'申请成功，请等待系统审核！']);
        }else{
            apifasong(['zt'=>'0','msg'=>'申请失败，请稍后再试！']);
        }
    }

    //重置secret
    //https://open.omechain.co/app/index/reset_secret/
    //{key:3b1bd31b702d8c217c1326de8266b95d}
    public function reset_secret(){
        $appdata=apijieshou();
        //合法性
        ishefa($appdata);
        //业务
        $secret_rand = apirand(50);     //50位随机数
        $secret_base = time().$secret_rand;   //原始值
        $secret = sha1(substr($secret_base,3,50));    //生成的secret sha1 40位
        $ret = db('app')->where('key',$appdata['key'])->update([
            'secret'=>$secret,
            'utime'=>time()
        ]);
        if($ret){
            apifasong(['zt'=>'1','msg'=>'刷新成功','secret'=>$secret]);
        }else{
            apifasong(['zt'=>'0','msg'=>'刷新失败，请稍后再试！']);
        }
    }
}
